﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
using System.Windows.Browser;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;

namespace SkyDriveConnector
{
    public static class SPHelpers
    {

        public static string GetUrl()
        {
            //string url = Application.Current.Host.Source.AbsoluteUri.Substring(0, Application.Current.Host.Source.AbsoluteUri.LastIndexOf("/"));
            //return url.Substring(0, url.LastIndexOf("/"));
            //return "https://www.anildwa.com/sites/testsite2";
            return (string) HtmlPage.Window.Invoke("GetSiteCollectionUrl");
        }


    }
    public static class SerializationHelper
    {
        public static object ToUserDefinedType<T>(this string SerializedObject)
        {
            StringReader reader = new StringReader(SerializedObject);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(reader);
        }


        public static string ToString<T>(this T obj)
        {
            StringWriter writer = new StringWriter();

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(writer, obj);

            return writer.ToString();

        }
    }

    public static class  ExtensionHelpers
    {
        public static TreeViewItem ContainerFromItem(this TreeView treeView, object item)
        {
            TreeViewItem containerThatMightContainItem = (TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(item);
            if (containerThatMightContainItem != null)
                return containerThatMightContainItem;
            else
                return ContainerFromItem(treeView.ItemContainerGenerator, treeView.Items, item);
        }

        public static ObservableCollection<SkyDriveObject> SortByObjectKey(this ItemCollection items, string SortOrder, Func<object,string> keySelector)
        {
            ObservableCollection<SkyDriveObject> tempcoll = new ObservableCollection<SkyDriveObject>();

            if (SortOrder == "Asc")
            {
                foreach (SkyDriveObject obj in items.OrderBy(keySelector))
                {
                    tempcoll.Add(obj);
                }
            }
            else
            {
                foreach (SkyDriveObject obj in items.OrderByDescending(keySelector))
                {
                    tempcoll.Add(obj);
                }
            }

            return tempcoll;

        }

        private static TreeViewItem ContainerFromItem(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, object item)
        {
            foreach (object curChildItem in itemCollection)
            {
                TreeViewItem parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(curChildItem);
                TreeViewItem containerThatMightContainItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);
                if (containerThatMightContainItem != null)
                    return containerThatMightContainItem;
                TreeViewItem recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
                if (recursionResult != null)
                    return recursionResult;
            }
            return null;
        }
    }

    public class JSONHelper
    {
        public static string Serialize<T>(T obj)
        {
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, obj);
            ms.Position = 0;
            var reader = new StreamReader(ms);
            string retVal = reader.ReadToEnd();

            ms.Dispose();
            return retVal;
        }

        public static T Deserialize<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(HttpUtility.HtmlDecode(json)));
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            obj = (T)serializer.ReadObject(ms);
            ms.Close();
            ms.Dispose();
            return obj;
        }
    }
}
